package com.stu.day07;

/**
 * @author zy
 * @date 2025/1/27 18:57
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;

public class Library {
    private List<Book> books = new ArrayList<>();

    // 1. 添加书籍
    public void addBook(Book book) {
        books.add(book);
        System.out.println("Book added: " + book);
    }

    // 2. 删除书籍
    public void removeBookByTitle(String title) {
        books.removeIf(book -> book.getTitle().equals(title));
        System.out.println("Book with title '" + title + "' removed.");
    }

    // 3. 打印所有书籍
    public void printAllBooks() {
        books.forEach(System.out::println);
    }

    // 4. 按价格排序书籍
    public void sortBooksByPrice() {
        books.sort(Comparator.comparingDouble(Book::getPrice));
        System.out.println("Books sorted by price:");
        printAllBooks();
    }

    // 5. 按书名排序书籍（Lambda表达式）
    public void sortBooksByTitle() {
        books.sort((b1, b2) -> b1.getTitle().compareTo(b2.getTitle()));
        System.out.println("Books sorted by title:");
        printAllBooks();
    }

    // 6. 按价格降序排序书籍
    public void sortBooksByPriceDesc() {
        books.sort((b1, b2) -> Double.compare(b2.getPrice(), b1.getPrice()));
        System.out.println("Books sorted by price (desc):");
        printAllBooks();
    }

    // 内部类：用于定义不同的排序策略
    public class SortByTitleAsc implements Comparator<Book> {
        @Override
        public int compare(Book b1, Book b2) {
            return b1.getTitle().compareTo(b2.getTitle());
        }
    }

    // 使用内部类来排序书籍
    public void sortBooksUsingInternalClass() {
        books.sort(new SortByTitleAsc());
        System.out.println("Books sorted using internal class:");
        printAllBooks();
    }
}

